﻿@model Domain.RoleInfo
@{
	ViewBag.Title = "角色授权 - " + Model.Name;
	Layout = "~/Areas/Admin/Views/Shared/_AdminContent.cshtml";
}
@section head {
	<link href="~/dist/ztree_v3/css/metroStyle/metroStyle.css" rel="stylesheet" />
}
@section body {
	<script src="~/dist/ztree_v3/js/jquery.ztree.all.min.js"></script>
	<script>
		// start 菜单权限树
		var setting = {
			view: {
				selectedMulti: false
			},
			check: {
				enable: true
			},
			data: {
				simpleData: {
					enable: true
				}
			},
			edit: {
				enable: false
			}
		};

		var zNodes = null;

		$(document).ready(function () {
			loadTree();
		});

		function loadTree() {
			$.ajax({
				url: "@Url.Action("GetRole_MenuAndFunc_Tree", "RoleInfo", new { area = "Admin" })",
				type: "post",
				data: { id: @Model.ID },
				dataType: "json",
				success: function (data) {
					zNodes = data;
					$.fn.zTree.init($("#js-tree-area"), setting, zNodes);
					var treeObj = $.fn.zTree.getZTreeObj("js-tree-area");
					window.treeObj = treeObj;
				}
			});
		}
		// end 菜单权限树

		// start 控制面板
		$("#js-btn-save").on("click", function () {
			var selectedNodes = treeObj.getCheckedNodes(true);
			var selectedMenuIds =  "", selectedFuncIds = "";
			for (var i = 0; i < selectedNodes.length; i++) {
				var node = selectedNodes[i];
				if (node.id == -1) {
					selectedFuncIds += node.fId + ",";
				} else {
					selectedMenuIds += node.id + ",";
				}
			}
			console.log("menu:" + selectedMenuIds);
			console.log("-----------------------------------------");
			console.log("func:" + selectedFuncIds);

			$.ajax({
				url: "@Url.Action("AssignPower", "RoleInfo", new { area = "Admin" })",
				type: "post",
				data: { id: @Model.ID, menuIds: selectedMenuIds, funcIds: selectedFuncIds },
				dataType: "json",
				success: function (data) {
					$("#js-message").fadeIn();
					if (data.code <= 0) {
						$("#js-message").attr("class", "alert alert-danger").find(".message").html(data.message)
					} else {
						$("#js-message").attr("class", "alert alert-success").find(".message").html(data.message);
						loadTree();
					}
					// 3s 后隐藏
					setTimeout(function () {
						$("#js-message").fadeOut("slow");
					}, 3000);
				}
			});
		});
		// end 控制面板
	</script>
}

<!-- start 提示框 -->
@Html.Partial("_MessagePartial")
<!-- end 提示框 -->
<!-- start 控制面板 -->
<div class="panel panel-default">
	<div class="panel-heading">
		<h3 class="panel-title">
			授权 - @Model.Name
		</h3>
	</div>
	<div class="panel-body">
		<div class="btn-toolbar" role="toolbar">
			<div class="btn-group">
				<a class="btn btn-default" href="javascript:history.go(-1);">返回</a>
				<button id="js-btn-save" class="btn btn-primary" type="button">保存</button>
			</div>
		</div>
		<hr />
		<!-- start 菜单权限树 -->
		<div id="js-menu-content" class="center-block" style="width:95%;border:1px solid rgb(170,170,170);z-index:10;">
			<ul id="js-tree-area" class="ztree" style="margin-top:0; width:100%; height:auto;"></ul>
		</div>
		<!-- end 菜单权限树 -->
	</div>
</div>
<!-- end 控制面板 -->
